<?php
namespace Modules\Admin\Model\Dao\Api;


class Resource {

    const RESOURCE_TYPE_MENU = '01';     // 菜单资源
    const RESOURCE_TYPE_BUTTON = '02';   // 按钮资源
    const RESOURCE_TYPE_FIELD = '03';    // 字段资源

    const PID_TYPE_APP = '01';           // 上级菜单类型-系统
    const PID_TYPE_MENU = '02';          // 上级菜单类型-菜单
    const PID_TYPE_BUTTON = '03';        // 上级菜单类型-按钮

    /**
     * 资源同步(新增和修改)
     *
     * @param string $resource_type  资源类型
     * @param string $id  编号
     * @param string $pid_type 资源类型
     * @param string $order 显示顺序
     * @return bool
     * @throws \Base\Exception\Service
     */
    public function sync($resource_type, $id, $name, $pid_type, $order, $pid = null) {
        $new_resource = array(
            array(
                'id'      => $id,
                'name'    => $name,
                'pidType' => $pid_type,
                'order'   => $order,
                'pid'     => $pid
            )
        );
        $data = array(
            'resourceType' => $resource_type,
            'newResource' => json_encode($new_resource, true),
        );

        $util = new Util();
        $ret  = $util->request("service/sync/resource", $data);
        if ($ret === false) {
            $code    = $util->getCode();
            $message = $util->getMessage();
            throw new \Base\Exception\Service($message, $code);
        } else {
            return true;
        }
    }

    /**
     * 资源同步(删除)
     * @param $resource_type
     * @param $delete_source
     * @return bool
     * @throws \Base\Exception\Service
     * @throws \S\Exception
     */

    public function delete($resource_type, $delete_source){
        $data = array(
            'resourceType' => $resource_type,
            'delResource'  => $delete_source
        );
        $util = new Util();
        $ret  = $util->request("service/sync/resource", $data);
        if ($ret === false) {
            $code    = $util->getCode();
            $message = $util->getMessage();
            throw new \Base\Exception\Service($message, $code);
        } else {
            return true;
        }
    }

}